package com.qf.time;

import com.qf.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class TimeTask {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ISearchService searchService;

    /**
     * 每10秒钟触发一次 - Quratz
     *
     * 秒 分 时 天 月 (周) 年
     *
     */
    @Scheduled(cron = "0/10 * * * * *")
    public void djlTask(){
        //从redis中获取所有的djl
        Map<Object, Object> djl = redisTemplate.opsForHash().entries("djl");

        for (Map.Entry<Object, Object> entry : djl.entrySet()) {
            //获得酒店ID
            Integer hid = Integer.parseInt(entry.getKey() + "");
            //获得点击数量
            Integer num = Integer.parseInt(entry.getValue() + "");

            //修改到es中
            searchService.updateHotalDjl(hid, num);
        }

        //从redis中删除信息
        redisTemplate.delete("djl");
    }
}
